package com.mcptt.db.model;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

import java.io.Serializable;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.DbConst;

public class AddrInfo implements Serializable
{
	public static final String Sys_Num = "0000000000";
	//	public static final String KEY = "addrInfo";
	public static final int GroupType_Crsc = 1;
	public static final int GroupType_Normal = 2;
	public static final int GroupType_Incomplete = 9;

	public static final int UserType_Sys = 10;
	public static final int UserType_Dac = 11;
	public static final int UserType_User = 12;
	public static final int UserType_Func = 13;
	public static final int UserType_Camera = 14;

	public static final int UserType_Ai_Car = 15;
	public static final int UserType_Ai_Plane = 16;
	public static final int UserType_Default = 19;
	public static final int Type_Error = 999;

	public static final int MembType_Normal = 21;

	public static final int DEFAULT_ATTR = 0;

	private static String domain = "crsc.mcx.mnc020.mcc460.5gr";//BootStrapSvrInfo.Default_Svr_Ip +":"+ BootStrapSvrInfo.Default_Svr_Port;
	protected int type;
	protected String addrId;
	protected String name;
	//	protected String opNum;
	protected int attr = 0;


	public static void setDomain(String url) {
		domain =url;
	}

	public AddrInfo()
	{

	}
	public AddrInfo(int type,String name,String mcId,int attr)
	{
		this.type = type;
		this.addrId = mcId;
		if(name == null
				|| name.trim().length() <= 0)
		{
			this.name = mcId;
		}
		else
		{
			this.name = name;
		}
		this.attr = attr;
	}
	public AddrInfo(Cursor c)
	{
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}


	public boolean isGroupType()
	{
		if(type == AddrInfo.GroupType_Crsc
				|| type == AddrInfo.GroupType_Normal
				|| type == AddrInfo.GroupType_Incomplete)
		{
			return true;
		}
		return false;
	}

	public boolean isUserType()
	{
		if(type == AddrInfo.UserType_Sys
				|| type == AddrInfo.UserType_Dac
				|| type == AddrInfo.UserType_User)
		{
			return true;
		}
		return false;
	}
	public int getType() {
		return type;
	}

	public void setType(int userType) {
		this.type = userType;
	}

	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}



	public void setAddrId(String addrId) {
		this.addrId = addrId;
	}

	public String getAddrId() {
		return addrId;
	}

	public String getMcxId() {
		return AddrInfo.toSipUrlFromNum(addrId);
	}

//	protected void updateOpNum(String num) {
//		this.opNum = num;
//	}
//	public String getOpNum() {
//		return opNum;
//	}

	public String getDscp()
	{
		if(name != null
				&& name.length() > 0)
		{
			return name;
		}
		if(addrId != null
				&& addrId.length() > 0)
		{
			return addrId;
		}
		else
		{
			return "";
		}
	}
	public void setIsAffiliation(boolean isAffiliatio)
	{
		if(isAffiliatio)
		{
			attr =1;
		}
		else
		{
			attr = 0;
		}
	}
	public boolean isAffiliation()
	{
		if(attr ==1)
		{
			return true;
		}
		return false;
	}
	public static String getDefaultName(String mcId)
	{
		return "User_" + mcId;
	}
	public int getAttr() {
		return attr;
	}

	public void setAttr(int attr) {
		this.attr = attr;
	}

	public boolean isSys() {
		if(type == UserType_Sys)
		{
			return true;
		}
		return false;
	}
	public boolean isDac() {
		if(type == UserType_Dac)
		{
			return true;
		}
		return false;
	}
	public boolean isCamera() {
		if(type == UserType_Camera)
		{
			return true;
		}
		return false;
	}
	public boolean isAi() {
		if(type == UserType_Ai_Car
				|| type == UserType_Ai_Plane)
		{
			return true;
		}
		return false;
	}
	public boolean isSameOne(AddrInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.addrId;
		if(this.addrId != null
				&& this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(UserInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.getAddrId();
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(McIdInfo mcIdInfo)
	{
		if(mcIdInfo == null)
		{
			return false;
		}
		String oldMcId = mcIdInfo.getAddrId();
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String oldMcId)
	{
		Log.debug(this.getClass(),"isSameOne addrId =  "+ addrId + " oldMcId =  "+ oldMcId);
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		else
		{
			Log.debug(this.getClass(),"isSameOne no sameone");
		}
		return false;
	}

	public boolean isSameAddrId(String oldMcId)
	{
		Log.debug(this.getClass(),"isSameAddrId addrId =  "+ addrId + " oldMcId =  "+ oldMcId);
		if(this.addrId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isEmergencyNum()
	{
		if(this.addrId.startsWith("50")
				&& this.addrId.endsWith("299"))
		{
			return true;
		}
		return false;
	}
	public static boolean isEmergencyNum(String groupId)
	{
		if(groupId == null)
		{
			return false;
		}
		else if(groupId.startsWith("50")
				&& groupId.endsWith("299"))
		{
			return true;
		}
		return false;
	}

	public static boolean isGroupNum(String groupId)
	{
		if(groupId == null)
		{
			return false;
		}
		else if(groupId.startsWith("59")
				&& groupId.length() == 10)
		{
			return true;
		}
		return false;
	}

	public static boolean isDacNum(String num)
	{
		if(num == null)
		{
			return false;
		}
		else if(num.length() == 8
				&& num.startsWith("7"))
		{
			return true;
		}
		return false;
	}

	public boolean isCall210()
	{
		if(this.addrId.startsWith("50")
				&& this.addrId.endsWith("210"))
		{
			return true;
		}
		return false;
	}

	public static boolean isFuncNum(String num)
	{
		if(num != null)
		{
			if(num.length() == 4
					&& num.startsWith("1")) {
				return true;
			}
			else if(num.startsWith("2")
					|| num.startsWith("3")
					|| num.startsWith("4")
//				|| num.startsWith("50")
//				|| num.startsWith("51")
//					|| num.startsWith("52")
//					|| num.startsWith("53")
//					|| num.startsWith("54")
//					|| num.startsWith("55")
//					|| num.startsWith("56")
//					|| num.startsWith("57")
//					|| num.startsWith("58")
//					|| num.startsWith("59")
					|| num.startsWith("6")
					|| num.startsWith("91")
					|| num.startsWith("93"))
			{
				return true;
			}
		}
		return false;
	}
	public static boolean isFuncGroupNum(String num)
	{
		if(num != null)
		{
			if(num.startsWith("50")
					|| num.startsWith("51"))
//					|| num.startsWith("59"))
			{
				return true;
			}
		}
		return false;
	}

	public boolean isFuncType()
	{
//		if(type == GroupType_Crsc
//				|| type == UserType_Func)
		if(type == UserType_Func)
		{
			return true;
		}
		return false;
	}



	public static boolean isCall210(String groupId)
	{
		if(groupId == null)
		{
			return false;
		}
		else if(groupId.startsWith("50")
				&& groupId.endsWith("210"))
		{
			return true;
		}
		return false;
	}
	public GroupInfo toGroupInfo()
	{
		return new GroupInfo(type,name, addrId,0 );
	}

	public GroupMembInfo toGroupCallMemberInfo(String groupAddrId)
	{
		return new GroupMembInfo(groupAddrId,addrId,name,addrId,attr);
	}

	public static String[] getColumns() {
		return new String[]{DbConst.COL_AddrId,DbConst.COL_Type,DbConst.COL_Name,DbConst.COL_Attr};
	}
	public ContentValues getSetDefaultGroupInfoContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_AddrId, addrId);
//		cv.put(DbConst.COL_Type,type);
//		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_Attr, attr);

		return cv;
	}

	public void createFromContentValue(ContentValues args) {

//		Log.debug(this.getClass(), "createFromContentValue");
		String tmp_s;

		tmp_s = args.getAsString(DbConst.COL_AddrId);
		if(tmp_s != null) {
			addrId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			type = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Attr);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				attr = value;
			}
		}
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("AddrInfo ");
		strBuf.append("type = ").append(type).append(",");
		strBuf.append("addrId = ").append(addrId).append(",");
		strBuf.append("name = ").append(name).append(",");
		strBuf.append("attr = ").append(attr).append(".");

		return strBuf.toString();
	}


	protected static final String BeginKey = "sip:";
	protected static final String EndKey = "@";

	public static String toImsImpi(String num,String domain)
	{
//		Log.i(THIS_FILE, "toSipNum num = " +num + " endStr = " + endStr);
		StringBuffer strBuf = new StringBuffer();
		strBuf.append(num);
		strBuf.append(EndKey);
		strBuf.append(domain);

		Log.debug(AddrInfo.class, "toSipNum = " +strBuf.toString() );

		return strBuf.toString();
	}

	public static String toImsImpu(String num,String domain)
	{
		Log.debug(AddrInfo.class, "toImsImpu num = " +num);
		StringBuffer strBuf = new StringBuffer(BeginKey);
		strBuf.append(num);
		strBuf.append(EndKey);
		strBuf.append(domain);

		Log.debug(AddrInfo.class, "toImsImpu = " +strBuf.toString() );
		return strBuf.toString();
	}

	public static String toMcxPsi(String keyStr,String domain)
	{
		Log.debug(AddrInfo.class, "toMcxPsi keyStr = " + keyStr + " domain = " + domain);
		if(keyStr == null
				|| keyStr.length() <= 0)
		{
			return "";
		}
		if(domain == null
				|| domain.length() <= 0)
		{
			return "";
		}
		StringBuffer strBuf = new StringBuffer(BeginKey);
		strBuf.append(keyStr).append(EndKey).append(domain);
		return strBuf.toString();//去掉前面�?0
	}

	public static String toNumFromSipUrl(String urlStr)
	{
		Log.debug(AddrInfo.class, "convertSipUrlToNum urlStr = " + urlStr);
		if(urlStr == null
				|| urlStr.length() <= 0)
		{
			return urlStr;
		}
		int beginIndex = urlStr.indexOf(BeginKey);
		int endIndex = urlStr.indexOf(EndKey);
		if(beginIndex == -1
				|| endIndex == -1
				|| beginIndex > endIndex)
		{
			return urlStr;
		}
		beginIndex = beginIndex + BeginKey.length();
		urlStr = urlStr.substring(beginIndex, endIndex);
		Log.debug(AddrInfo.class, "convertSipUrlToNum end urlStr = " + urlStr);
		return urlStr;//去掉前面�?0
	}
	protected static final String SessionBeginKey = "<";
	protected static final String SessionEndKey = ">";
	public static String toSessionIdFormRemoteContact(String urlStr)
	{
		Log.debug(AddrInfo.class, "toSessionIdFormRemoteContact urlStr = " + urlStr);
		if(urlStr == null
				|| urlStr.length() <= 0)
		{
			return "";
		}
		int beginIndex = urlStr.indexOf(SessionBeginKey);
		int endIndex = urlStr.indexOf(SessionEndKey);
		if(beginIndex == -1
				|| endIndex == -1
				|| beginIndex > endIndex)
		{
			return urlStr;
		}
		beginIndex = beginIndex + SessionBeginKey.length();
		urlStr = urlStr.substring(beginIndex, endIndex);
		Log.debug(AddrInfo.class, "toSessionIdFormRemoteContact end urlStr = " + urlStr);
		return urlStr;//去掉前面�?0
	}

	public static String toSipUrlFromNum(String num)
	{
		StringBuffer strBuf = new StringBuffer("<");
		strBuf.append(BeginKey).append(num).append(EndKey).append(domain).append(">");
		return strBuf.toString();
	}

	public static String toUrlFromNum(String num)
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append(BeginKey).append(num).append(EndKey).append(domain);
		return strBuf.toString();
	}
	public static int getUserTypeByNum(String numStr)
	{
		if(numStr == null
			|| numStr.equals(""))
		{
			return AddrInfo.Type_Error;
		}
		boolean isMatchDispatcher = numStr.matches("^7([0-9]{2})[0-9]{5}$");
		boolean isMatchUser = numStr.matches("^1498([0-9]{2})[0-9]{5}$");
		boolean isMatchGroup = numStr.matches("^59([0-9]{1})([0-9]{2})[0-9]{5}$");
		if(numStr.length() == 3)
		{
			return AddrInfo.GroupType_Crsc;
		}
		else if(numStr.length() == 5
				&& AddrInfo.isFuncGroupNum(numStr))
		{
			return AddrInfo.GroupType_Crsc;
		}
		else if(isMatchGroup)//群组
		{
			return AddrInfo.GroupType_Normal;
		}
		else if(AddrInfo.isFuncNum(numStr))
		{
			return AddrInfo.UserType_Func;
		}
		else if(isMatchDispatcher)//群组
		{
			return AddrInfo.UserType_User;
		}
		else if(isMatchUser)//群组
		{
			return AddrInfo.UserType_User;
		}
		return AddrInfo.Type_Error;
	}
}
